1
|
|
|
/** |
2
|
|
|
* @author Piotr Mrowczynski <[email protected]> |
3
|
|
|
* |
4
|
|
|
* @copyright Copyright (c) 2017, Piotr Mrowczynski. |
5
|
|
|
* @license AGPL-3.0 |
6
|
|
|
* |
7
|
|
|
* This code is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
9
|
|
|
* as published by the Free Software Foundation. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU Affero General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
var Files_PaperHive; |
22
|
|
|
Files_PaperHive = { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Holds the notification container |
26
|
|
|
*/ |
27
|
|
|
$notification: null, |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Holds the notification html |
31
|
|
|
*/ |
32
|
|
|
container: null, |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Holds the notification html options |
36
|
|
|
*/ |
37
|
|
|
containerOptions: null, |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Gets if is paperhive file |
41
|
|
|
*/ |
42
|
|
|
isPaperHive: function (fileName) { |
43
|
|
|
var parts = fileName.split('.'); |
44
|
|
|
var extension = ""; |
45
|
|
|
if (parts.length > 1) { |
46
|
|
|
extension = parts.pop(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return (extension === 'paperhive'); |
50
|
|
|
|
51
|
|
|
}, |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Registers the file actions |
55
|
|
|
*/ |
56
|
|
|
registerFileActions: function () { |
57
|
|
|
var mimetype = 'application/octet-stream'; |
58
|
|
|
|
59
|
|
|
OCA.Files.fileActions.registerAction({ |
|
|
|
|
60
|
|
|
name: 'ShowPaper', |
61
|
|
|
displayName: '', |
62
|
|
|
altText: t('core', 'Show Paper'), |
63
|
|
|
mime: mimetype, |
64
|
|
|
actionHandler: _.bind(this._onPaperHiveTrigger, this), |
|
|
|
|
65
|
|
|
permissions: OC.PERMISSION_READ, |
|
|
|
|
66
|
|
|
iconClass: 'icon-filetype-paperhive', |
67
|
|
|
type: OCA.Files.FileActions.TYPE_INLINE, |
68
|
|
|
render: function (actionSpec, isDefault, context) { |
69
|
|
|
if (OCA.Files_PaperHive.isPaperHive(context.$file.attr('data-file'))) { |
|
|
|
|
70
|
|
|
return OCA.Files.fileActions._defaultRenderAction.call(OCA.Files.fileActions, actionSpec, isDefault, context); |
71
|
|
|
} |
72
|
|
|
// don't render anything |
73
|
|
|
return null; |
74
|
|
|
} |
75
|
|
|
}); |
76
|
|
|
}, |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Setup on page load |
80
|
|
|
*/ |
81
|
|
|
initialize: function () { |
82
|
|
|
$(document).bind('mouseup', this._onClickDocument); |
83
|
|
|
this.$notification = null; |
84
|
|
|
this.registerFileActions(); |
85
|
|
|
}, |
86
|
|
|
|
87
|
|
|
createContainer: function () { |
88
|
|
|
var self = this; |
89
|
|
|
$.ajax({ |
90
|
|
|
type: 'GET', |
91
|
|
|
url: OC.generateUrl('/apps/files_paperhive/ajax/getpaperhivedetails') |
|
|
|
|
92
|
|
|
}) |
93
|
|
|
.done(function (phdata) { |
94
|
|
|
var containerString = '<div class="icon-paperhive"></div>' + |
95
|
|
|
'<div><p class="normal">Visit PaperHive at </p><p class="normal">' + phdata.paperhive_base_url + '</p><p class="normal"> and transform reading into a process of collaboration!</p></div>' + |
96
|
|
|
//'<div><span></span></div>' + |
97
|
|
|
'<div><p class="bold">Your Book ID </p><p class="normal">is the last fragment of PaperHive document URL.</p></div>' + |
98
|
|
|
'<div><p class="normal">Example: </p><p class="normal">' + phdata.paperhive_base_url + phdata.paperhive_document_url + '</p><p class="bold">Ra5WnkxImoOE</p></div>'; |
99
|
|
|
|
100
|
|
|
self.container = $('<div class="notification_paperhive"></div>').html( |
101
|
|
|
containerString |
102
|
|
|
); |
103
|
|
|
self.containerOptions = { |
104
|
|
|
isHTML: true, |
105
|
|
|
timeout: 30 |
106
|
|
|
}; |
107
|
|
|
self.$notification = OC.Notification.showHtml( |
|
|
|
|
108
|
|
|
self.container, |
109
|
|
|
self.containerOptions |
110
|
|
|
); |
111
|
|
|
}) |
112
|
|
|
.fail(function (jqXHR) { |
113
|
|
|
var message; |
114
|
|
|
|
115
|
|
|
try { |
116
|
|
|
message = JSON.parse(jqXHR.responseText).message; |
117
|
|
|
} catch (e) { |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
OC.dialogs.alert(message, t('files_paperhive', 'An error occurred!')); |
|
|
|
|
121
|
|
|
}); |
122
|
|
|
}, |
123
|
|
|
|
124
|
|
|
createNotification: function () { |
125
|
|
|
if (this.$notification === null) { |
126
|
|
|
this.createContainer(); |
127
|
|
|
} |
128
|
|
|
}, |
129
|
|
|
|
130
|
|
|
hideNotification: function () { |
131
|
|
|
if (!OC.Notification.isHidden() && this.$notification != null) { |
|
|
|
|
132
|
|
|
OC.Notification.hide(this.$notification); |
133
|
|
|
this.$notification = null; |
134
|
|
|
} |
135
|
|
|
}, |
136
|
|
|
|
137
|
|
|
failureNotification: function (message) { |
138
|
|
|
OC.dialogs.alert(message, t('files_paperhive', 'An error occurred!')); |
|
|
|
|
139
|
|
|
}, |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Loads the data through AJAX |
143
|
|
|
*/ |
144
|
|
|
loadFile: function (dir, filename, fetchDiscussions, success, failure) { |
145
|
|
|
$.get( |
146
|
|
|
OC.generateUrl('/apps/files_paperhive/ajax/loadfile'), |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
filename: filename, |
149
|
|
|
dir: dir, |
150
|
|
|
fetchDiscussions: fetchDiscussions |
151
|
|
|
} |
152
|
|
|
).done(function (fileContents) { |
153
|
|
|
// Call success callback |
154
|
|
|
success(fileContents); |
155
|
|
|
}).fail(function (jqXHR) { |
156
|
|
|
var message; |
157
|
|
|
|
158
|
|
|
try { |
159
|
|
|
message = JSON.parse(jqXHR.responseText).message; |
160
|
|
|
} catch (e) { |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
failure(message); |
164
|
|
|
}); |
165
|
|
|
}, |
166
|
|
|
|
167
|
|
|
validatePaperHiveJSON: function (paperHiveObject) { |
168
|
|
|
//validate request |
169
|
|
|
var successResponseTags = ['id', 'authors', 'title']; |
170
|
|
|
var errorResponseTags = ['status', 'message']; |
171
|
|
|
for (var responseErrorTag in errorResponseTags) { |
|
|
|
|
172
|
|
|
if (paperHiveObject.hasOwnProperty(errorResponseTags[responseErrorTag])) { |
173
|
|
|
return false; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
for (var responseSuccessTag in successResponseTags) { |
|
|
|
|
178
|
|
|
if (!paperHiveObject.hasOwnProperty(successResponseTags[responseSuccessTag])) { |
179
|
|
|
return false; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return true; |
184
|
|
|
}, |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Handles request for book contents to PaperHive API |
188
|
|
|
*/ |
189
|
|
|
getPaperHiveBook: function (dir, bookID, success, failure) { |
190
|
|
|
$.get( |
191
|
|
|
OC.generateUrl('/apps/files_paperhive/ajax/getpaperhivedocument'), |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
dir: dir, |
194
|
|
|
bookID: bookID |
195
|
|
|
} |
196
|
|
|
).done(function (paperHiveData) { |
197
|
|
|
// Success - found valid Book at PaperHive |
198
|
|
|
success(paperHiveData); |
199
|
|
|
}).fail(function (jqXHR) { |
200
|
|
|
var message; |
201
|
|
|
|
202
|
|
|
try { |
203
|
|
|
message = JSON.parse(jqXHR.responseText).message; |
204
|
|
|
} catch (e) { |
|
|
|
|
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
failure("Error occured while connecting to PaperHive: " + message); |
208
|
|
|
}); |
209
|
|
|
|
210
|
|
|
}, |
211
|
|
|
|
212
|
|
|
setDiscussionCount: function ($tr) { |
213
|
|
|
OCA.Files_PaperHive.loadFile( |
|
|
|
|
214
|
|
|
$tr.attr('data-path'), |
215
|
|
|
$tr.attr('data-file'), |
216
|
|
|
"true", |
217
|
|
|
function (paperHiveData) { |
218
|
|
|
var discussionsCount = paperHiveData.paperhive_discussion_count; |
219
|
|
|
OCA.Files_PaperHive._updatePaperHiveFileData($tr, discussionsCount); |
|
|
|
|
220
|
|
|
}, |
221
|
|
|
function (message) { |
|
|
|
|
222
|
|
|
} |
223
|
|
|
); |
224
|
|
|
}, |
225
|
|
|
|
226
|
|
|
_updatePaperHiveFileData: function ($tr, discussionCount) { |
227
|
|
|
$tr.find('.filename .thumbnail').css('background-image', 'url(' + OC.imagePath('files_paperhive', 'paperhive-icon') + ')'); |
|
|
|
|
228
|
|
|
var action = $tr.find('.fileactions .action[data-action="ShowPaper"]'); |
229
|
|
|
|
230
|
|
|
action.addClass('shared-style'); |
231
|
|
|
var icon = action.find('.icon'); |
232
|
|
|
|
233
|
|
|
var message = ''; |
234
|
|
|
if (discussionCount === -1) { |
235
|
|
|
message = t('files_paperhive', 'Discuss'); |
236
|
|
|
} else { |
237
|
|
|
message = t('files_paperhive', 'Discuss') + ' (' + discussionCount + ')'; |
238
|
|
|
} |
239
|
|
|
action.html('<span> ' + message + '</span>').prepend(icon); |
240
|
|
|
}, |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Handles the FileAction click event |
244
|
|
|
*/ |
245
|
|
|
_onPaperHiveTrigger: function (filename, context) { |
246
|
|
|
// Get the file data |
247
|
|
|
this.loadFile( |
248
|
|
|
context.dir, |
249
|
|
|
filename, |
250
|
|
|
"false", |
251
|
|
|
function (paperHiveData) { |
252
|
|
|
try { |
253
|
|
|
var paperHiveObject = JSON.parse(paperHiveData.paperhive_document); |
254
|
|
|
} catch (e) { |
255
|
|
|
OC.dialogs.alert("Your [.paperhive] file is not a valid PaperHive document, please redownload document"); |
|
|
|
|
256
|
|
|
return; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
if (!OCA.Files_PaperHive.validatePaperHiveJSON(paperHiveObject)) { |
|
|
|
|
260
|
|
|
OC.dialogs.alert("Your [.paperhive] file is not a valid PaperHive document, please redownload document"); |
261
|
|
|
return; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
var paperhiveUrl = paperHiveData.paperhive_base_url + paperHiveData.paperhive_document_url + paperHiveObject.id; |
265
|
|
|
|
266
|
|
|
var w = window.open(paperhiveUrl, '_blank'); |
267
|
|
|
if (!w) { |
268
|
|
|
window.location.href = paperhiveUrl; |
269
|
|
|
} |
270
|
|
|
}, |
271
|
|
|
function (message) { |
272
|
|
|
// Oh dear |
273
|
|
|
OC.dialogs.alert(message, t('files_paperhive', 'An error occurred!')); |
|
|
|
|
274
|
|
|
} |
275
|
|
|
); |
276
|
|
|
}, |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Handles event when clicking outside editor |
280
|
|
|
*/ |
281
|
|
|
_onClickDocument: function (event) { |
282
|
|
|
var menuItem = $(event.target).closest('.menuitem'); |
283
|
|
|
var notificationItem = $(event.target).closest('.notification_paperhive'); |
284
|
|
|
|
285
|
|
|
if (menuItem.length && menuItem.attr('data-action') === 'paperhive') { |
286
|
|
|
OCA.Files_PaperHive.createNotification(); |
|
|
|
|
287
|
|
|
} else if (!notificationItem.length) { |
288
|
|
|
OCA.Files_PaperHive.hideNotification(); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
}; |
292
|
|
|
|
293
|
|
|
Files_PaperHive.NewFileMenuPlugin = { |
294
|
|
|
|
295
|
|
|
attach: function (menu) { |
296
|
|
|
var fileList = menu.fileList; |
297
|
|
|
|
298
|
|
|
// only attach to main file list, public view is not supported yet |
299
|
|
|
if (fileList.id !== 'files') { |
300
|
|
|
return; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
// register the new menu entry |
304
|
|
|
menu.addMenuEntry({ |
305
|
|
|
id: 'paperhive', |
306
|
|
|
displayName: t('files_paperhive', 'PaperHive Book'), |
307
|
|
|
templateName: t('files_paperhive', 'Your Book ID'), |
308
|
|
|
iconClass: 'icon-filetype-paperhive', |
309
|
|
|
fileType: 'file', |
310
|
|
|
actionHandler: function (bookID) { |
311
|
|
|
// Hide any remaining notification |
312
|
|
|
OCA.Files_PaperHive.hideNotification(); |
|
|
|
|
313
|
|
|
|
314
|
|
|
// Get the file data from PaperHive API |
315
|
|
|
var dir = fileList.getCurrentDirectory(); |
316
|
|
|
var $saveNot = OC.Notification.showHtml(t('files_paperhive', 'Saving...')); |
|
|
|
|
317
|
|
|
OCA.Files_PaperHive.getPaperHiveBook( |
318
|
|
|
dir, |
319
|
|
|
bookID, |
320
|
|
|
function (paperHiveData) { |
321
|
|
|
var filename = paperHiveData.filename+paperHiveData.extension; |
322
|
|
|
|
323
|
|
|
fileList.filesClient.getFileInfo( |
324
|
|
|
paperHiveData.path, |
325
|
|
|
{ |
326
|
|
|
properties: fileList._getWebdavProperties() |
327
|
|
|
}) |
328
|
|
|
.then(function(status, data) { |
329
|
|
|
fileList.add(data, {scrollTo: true}); |
330
|
|
|
var $tr = fileList.findFileEl(filename); |
331
|
|
|
OCA.Files_PaperHive._updatePaperHiveFileData($tr, paperHiveData.discussionCount); |
|
|
|
|
332
|
|
|
OC.Notification.hide($saveNot); |
|
|
|
|
333
|
|
|
}) |
334
|
|
|
.fail(function(status) { |
|
|
|
|
335
|
|
|
OC.Notification.hide($saveNot); |
|
|
|
|
336
|
|
|
OCA.Files_PaperHive.failureNotification(t('files_paperhive', 'Please reload the page, error occured')); |
|
|
|
|
337
|
|
|
}); |
338
|
|
|
}, |
339
|
|
|
function (message) { |
340
|
|
|
OC.Notification.hide($saveNot); |
|
|
|
|
341
|
|
|
OCA.Files_PaperHive.failureNotification(message); |
|
|
|
|
342
|
|
|
} |
343
|
|
|
); |
344
|
|
|
} |
345
|
|
|
}); |
346
|
|
|
} |
347
|
|
|
}; |
348
|
|
|
|
349
|
|
|
Files_PaperHive.FileMenuPlugin = { |
350
|
|
|
|
351
|
|
|
attach: function (fileList) { |
352
|
|
|
// use delegate to catch the case with multiple file lists |
353
|
|
|
fileList.$el.on('fileActionsReady', function (ev) { |
354
|
|
|
var $files = ev.$files; |
355
|
|
|
var $phfiles = []; |
356
|
|
|
|
357
|
|
|
_.each($files, function (file) { |
|
|
|
|
358
|
|
|
var $tr = $(file); |
359
|
|
|
|
360
|
|
|
if (OCA.Files_PaperHive.isPaperHive($tr.attr('data-file'))) { |
|
|
|
|
361
|
|
|
OCA.Files_PaperHive._updatePaperHiveFileData($tr, -1); |
362
|
|
|
$phfiles.push(file); |
363
|
|
|
} |
364
|
|
|
}); |
365
|
|
|
|
366
|
|
|
setTimeout(function () { |
367
|
|
|
_.each($phfiles, function (file) { |
|
|
|
|
368
|
|
|
var $tr = $(file); |
369
|
|
|
OCA.Files_PaperHive.setDiscussionCount($tr); |
|
|
|
|
370
|
|
|
}); |
371
|
|
|
}, 20); |
372
|
|
|
|
373
|
|
|
}); |
374
|
|
|
} |
375
|
|
|
}; |
376
|
|
|
|
377
|
|
|
OCA.Files_PaperHive = Files_PaperHive; |
378
|
|
|
|
379
|
|
|
OC.Plugins.register('OCA.Files.NewFileMenu', Files_PaperHive.NewFileMenuPlugin); |
|
|
|
|
380
|
|
|
OC.Plugins.register('OCA.Files.FileList', Files_PaperHive.FileMenuPlugin); |
381
|
|
|
|
382
|
|
|
$(document).ready(function () { |
383
|
|
|
OCA.Files_PaperHive.initialize(); |
|
|
|
|
384
|
|
|
}); |
385
|
|
|
|
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.